package com.nanbei.queue;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: BaoShan.li
 * @Created: 2023/11/23 11:01
 */
public class LinkQueueTest {

    @Test
    public void test(){
        LinkQueue<Integer> queue = new LinkQueue<>(5);
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        queue.offer(5);

        printfNode(queue);
        System.out.println("----------------------");
        System.out.println(queue.peek());
        System.out.println("----------------------");
        printfNode(queue);
        System.out.println("----------------------");
        System.out.println(queue.poll());
        System.out.println("----------------------");
        printfNode(queue);

    }

    @Test
    public void tes1(){
        ArrayQueue<Integer> queue = new ArrayQueue<Integer>(5);
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        queue.offer(5);

        printfNode(queue);
        System.out.println("----------------------");
        System.out.println(queue.peek());
        System.out.println("----------------------");
        printfNode(queue);
        System.out.println("----------------------");
        System.out.println(queue.poll());
        System.out.println("----------------------");
        printfNode(queue);

    }

    @Test
    public void test2(){
        int i =32;
        int num =1;
        while ((1 << num) < i){
            num++;
        }
        System.out.println(1<<num);
    }

    private void printfNode(LinkQueue<Integer> queue) {
        for (Integer node : queue){
            System.out.println(node);
        }
    }

    private void printfNode(ArrayQueue<Integer> queue) {
        for (Integer node : queue){
            System.out.println(node);
        }
    }

    /**
     *  测试无序数据实现优先级队列
     */
    @Test
    public void test3(){
        DisorderQueue<Entry> entryQueue = new DisorderQueue<Entry>(4);
        entryQueue.offer(new Entry("test10",10));
        entryQueue.offer(new Entry("test3",3));
        entryQueue.offer(new Entry("test2",2));
        entryQueue.offer(new Entry("test9",9));
        Entry peek = entryQueue.poll();
        System.out.println(peek.value);
    }


    /**
     * 测试有序数组实现优先级队列
     */
    @Test
    public void test4(){
        OrderQueue<Entry> entryQueue = new OrderQueue<>(3);
        entryQueue.offer(new Entry("test10",10));
        entryQueue.offer(new Entry("test3",3));
        entryQueue.offer(new Entry("test2",2));
        entryQueue.offer(new Entry("test9",9));
        Entry peek = entryQueue.poll();
        System.out.println(peek.value);
    }

    /**
     * 阻塞队列  单锁实现
     */
    @Test
    public void test5(){
        SingleBlockingQueue<Integer> entryQueue = new SingleBlockingQueue<>(3);


        try {
            new Thread(()->{
                try {
                    Integer poll = entryQueue.poll();
                    System.out.println(poll);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println(e);
                }
            },"poll_1").start();

            new Thread(()->{
                try {
                    entryQueue.offer(5);


                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println(e);

                }
            },"offer_2").start();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);

        }

        System.out.println("主线程结束");
    }

    @Test
    public void test6(){
        SingleBlockingQueue<Integer> entryQueue = new SingleBlockingQueue<>(3);
        try {


            new Thread(()->{
                try {
                    TimeInterval timer = DateUtil.timer();
                    entryQueue.offer(4);
                    System.out.println(timer.interval());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"offer-1").start();



            new Thread(() -> {
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                     entryQueue.offer(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "poll-1").start();

            TimeUnit.MILLISECONDS.sleep(50000);


        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }




}
